package com.hj.ct.fragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.hj.ct.R;
import com.hj.ct.view.WheelDialog;
import com.hj.ct.view.WheelDialog.OnMyOnPickListener;

public class BarChartFragment extends BaseFragment implements OnClickListener {

	private BarChart1Fragment chart1Fragment;
	private BarChart2Fragment chart2Fragment;
	private BarChart3Fragment chart3Fragment;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_horizontalbarchart, null);
		view.findViewById(R.id.data_btn).setOnClickListener(this);
		view.findViewById(R.id.iv_back).setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				getActivity().finish();
			}
		});
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		if (chart1Fragment != null) {
			transaction.remove(chart1Fragment);
			transaction.remove(chart2Fragment);
			transaction.remove(chart3Fragment);
		}

		chart1Fragment = new BarChart1Fragment();
		chart2Fragment = new BarChart2Fragment();
		chart3Fragment = new BarChart3Fragment();
		transaction.add(R.id.fragment1, chart1Fragment);
		transaction.add(R.id.fragment2, chart2Fragment);
		transaction.add(R.id.fragment3, chart3Fragment);
		transaction.commit();
	}

	private long startTime = System.currentTimeMillis();

	private void initStartDate() {
		WheelDialog dialog = new WheelDialog(getActivity());
		// 设置默认位置为当前年月日
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(startTime));
		int thisYear = calendar.get(Calendar.YEAR);
		int thisMouth = calendar.get(Calendar.MONTH);
		String firstdata = "年,1902," + thisYear + ",false," + (thisYear - 1902);
		String seconddata = "月,1,12,true," + thisMouth;
		dialog.setData("起始时间", "deliver_at", firstdata, seconddata, null, null);

		dialog.setOnPickListener(new OnMyOnPickListener() {

			@Override
			public void onPickData(String key, String tag) {
				SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM");
				try {
					Date date = sFormat.parse(tag);
					if (date.getTime() > System.currentTimeMillis()) {
						toast("起始日期不能超过今天哦");
					} else {
						startTime = date.getTime();
						chart1Fragment.setCurrentTime(startTime);
						chart2Fragment.setCurrentTime(startTime);
						chart3Fragment.setCurrentTime(startTime);
					}
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		});
		dialog.show();
	}

	public void handlerUI(int action, int code, Object... obj) {

	}

	public void onClick(View v) {
		if (v.getId() == R.id.data_btn) {
			initStartDate();
		}
	}
}
